package com.qinglei.recoup.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qinglei.recoup.common.domain.Tree;
import com.qinglei.recoup.system.dao.RadarBedMapper;
import com.qinglei.recoup.system.domain.Bed;
import com.qinglei.recoup.system.domain.BedTreeAttachBO;
import com.qinglei.recoup.system.domain.Radar;
import com.qinglei.recoup.system.domain.RadarBed;
import com.qinglei.recoup.system.domain.vo.RadarBedVo;
import com.qinglei.recoup.system.service.BedService;
import com.qinglei.recoup.system.service.RadarBedService;
import com.qinglei.recoup.system.service.RadarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author syp
 */
@Service
public class RadarBedServiceImpl extends ServiceImpl<RadarBedMapper, RadarBed> implements RadarBedService {

    @Autowired
    private BedService bedService;
    @Autowired
    private RadarService radarService;

    @Override
    public Tree<Bed> getBedTree4BindRadar() {
        List<RadarBed> radarBeds = this.baseMapper.selectList(Wrappers.<RadarBed>lambdaQuery().select(RadarBed::getBedId));
        Set<Integer> bedIds = radarBeds.stream().map(RadarBed::getBedId).collect(Collectors.toSet());
        BedTreeAttachBO disabledData = new BedTreeAttachBO();
        disabledData.setBedIds(bedIds);
        Tree<Bed> bedTree = bedService.getBedTree(disabledData);
        return bedTree;
    }

    @Override
    public Integer getMonitorBedNum(Integer tenantId) {
        return this.baseMapper.getMonitorBedNum(tenantId);
    }

    @Override
    public List<Radar> queryRadarListByRoomName(String roomName) {
        return this.baseMapper.queryRadarListByRoomName(roomName);
    }

    @Override
    public List<Bed> queryBedListByRoomName(String roomName) {
        return this.baseMapper.queryBedListByRoomName(roomName);
    }

    @Override
    public Map<Integer, RadarBedVo> getRadarBedMapByBedIds(List<Integer> bedIds) {
        List<RadarBedVo> radarBedVoList = new ArrayList<>();
        Map<Integer, RadarBedVo> radarBedVoMapList = new HashMap<>();
        if (CollectionUtils.isEmpty(bedIds)) {
            return radarBedVoMapList;
        }
        List<RadarBed> radarBeds = baseMapper.selectList(new QueryWrapper<RadarBed>().lambda()
                .in(RadarBed::getBedId, bedIds).eq(RadarBed::getIsDeleted, 0));
        if (CollectionUtils.isEmpty(radarBeds)) {
            return radarBedVoMapList;
        }
        //雷达ID
        List<Integer> radarIds = radarBeds.stream().map(RadarBed::getRadarId).distinct().collect(Collectors.toList());
        List<Radar> radarList = radarService.list(new QueryWrapper<Radar>().lambda().in(Radar::getId, radarIds));
        if (CollectionUtils.isEmpty(radarList)) {
            return radarBedVoMapList;
        }
        Map<Integer, String> radarMapList = radarList.stream().collect(Collectors.toMap(Radar::getId, Radar::getSnCode));
        radarBeds.forEach(radarBed -> {
            RadarBedVo radarBedVo = new RadarBedVo();
            radarBedVo.setRadarId(radarBed.getRadarId());
            radarBedVo.setBedId(radarBed.getBedId());
            if (radarMapList.containsKey(radarBed.getRadarId())) {
                radarBedVo.setSnCode(radarMapList.get(radarBed.getRadarId()));
            }
            radarBedVoList.add(radarBedVo);
        });
        Map<Integer, RadarBedVo> collect = new HashMap<>();
        for (RadarBedVo radarBedVo : radarBedVoList) {
            collect.put(radarBedVo.getBedId(), radarBedVo);
        }
        return collect;
    }


}
